package com.example.controller.user;

import com.example.common.Result;
import com.example.entity.dto.UserDTO;
import com.example.entity.item.CartItem;
import com.example.entity.item.Carts;
import com.example.entity.item.Favorites;
import com.example.entity.item.FavoritesItem;
import com.example.entity.order.OrderItem;
import com.example.entity.user.User;
import com.example.entity.user.UserAddresses;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    //更新用户信息
    @PutMapping("/updateMySelf")
    public Result updateUser(@RequestBody User user) {

        return Result.success(userMapper.updateUser(user));
    }

    //获取用户信息
    @GetMapping("/GetUserInfo")
    public Result GetUserInfo(String uid) {
        UserDTO userDTO = userService.GetUserInfo(uid);
        return Result.success(userDTO);
    }

    //删除用户
    @DeleteMapping("/delete/{UID}")
    public Result deleteUserById(@PathVariable String UID) {
        userService.deleteUserById(UID);
        return Result.success();
    }

    //更新用户资料
    @PutMapping("/update")
    public Result updateUserById(@RequestBody User user) {
        userService.updateUserById(user);
        return Result.success();
    }

    //ID查询用户
    @GetMapping("/selectUserById/{UID}")
    public Result selectUserById(@PathVariable String UID) {
        User user = userService.selectUserById(UID);
        return Result.success(user);
    }

    //查询所有用户
    @GetMapping("/selectUserAll")
    public Result selectUserAll(User user) {
        List<User> list = userService.selectUserAll(user);
        return Result.success(list);
    }


    //分页查询用户
    @GetMapping("/selectUserPage")
    public Result selectUserPage(User user,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> page = userService.selectUserPage(user, pageNum, pageSize);
        return Result.success(page);
    }


    //查询用户所有收获地址
    @GetMapping("/selectUserAddressAll/{uid}")
    public Result selectUserAddressAll(@PathVariable String uid) {
        List<UserAddresses> userAddresses = userService.selectUserAddressAll(uid);
        return Result.success(userAddresses);
    }

    //添加收获地址
    @PostMapping("/AddUserAddress")
    public Result AddUserAddress(@RequestBody UserAddresses userAddresses) {
        userService.AddUserAddress(userAddresses);
        return Result.success();
    }

    //查询用户购物车
    @GetMapping("/selectUserCart/{UID}")
    public Result selectUserCart(@PathVariable String UID) {
        List<CartItem> cartItem = userService.selectUserCart(UID);
        return Result.success(cartItem);
    }

    //查询用户订单
    @GetMapping("/selectOrder/{UID}")
    public Result selectOrder(@PathVariable String UID) {
        List<OrderItem> orderItems = userService.selectOrder(UID);
        return Result.success(orderItems);
    }

    //加入购物车
    @PostMapping("/JoinCart")
    public Result JoinCart(@RequestBody Carts carts) {
        System.out.println(carts);
        if (carts == null) {
            return Result.error("参数不完整");
        }
        userService.JoinCart(carts);
        return Result.success();
    }

    //删除购物车商品
    @DeleteMapping("/DeleteCartItem/{cartID}")
    public Result DeleteCartItem(@PathVariable Integer cartID) {
        userService.DeleteCartItem(cartID);
        return Result.success(200);
    }

    //加入收藏夹
    @PostMapping("/JoinFav")
    public Result JoinFav(@RequestBody Favorites favorites) {
        userService.JoinFav(favorites);
        return Result.success();
    }

    //查询是否收藏
    @GetMapping("selectUserFavById/{UID}")
    public Result selectUserFavById(@PathVariable String UID, @RequestParam String itemId) {
        Boolean bool = userService.selectUserFavById(UID, itemId);
        if (bool != null) {
            return Result.success("已收藏");
        } else {
            return Result.success("未收藏");
        }
    }

    //查询用户收藏夹
    @GetMapping("selectUserFav/{UID}")
    public Result selectUserFav(@PathVariable String UID) {
        List<FavoritesItem> favoritesItems = userService.selectUserFav(UID);
        return Result.success(favoritesItems);
    }


}
